<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use think\Db;


class Growgrass extends api
{
	
	//如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['getGrass', 'getSheep', 'getMarket','dayTask','comfirmAnswer'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

	//获得草数
	public function getGrass(){
        //$uid = Request::instance()->header()['uid'];
        $uid = 2;
		$grass = Db::table('xmr_grass')->where('uid',$uid)->find();
		return ['grass'=>$grass['nowGrass']];
	}
	
    //获得羊数
	public function getSheep(){
      //$uid = Request::instance()->header()['uid'];
        $uid = 2;
		$grass = Db::table('xmr_grass')->where('uid',$uid)->find();
		return ['sheep'=>$grass['sheep']];
	}
  
	//获得赶集数
	public function getMarket(){
      //$uid = Request::instance()->header()['uid'];
        $uid = 2;
		$grass = Db::table('xmr_grass')->where('uid',$uid)->find();
		return ['marketTimes'=>$grass['marketTimes']];
	}
  
	//日推小任务
	public function dayTask()
	{   
      //$uid = Request::instance()->header()['uid'];
        $uid = 2;
		$grass = Db::table('xmr_grass')->where('uid',$uid)->find();
		$id = $grass['grassTotal'] + 1;
		$sql = 'select d.*,a.* from xmr_daytask d  join xmr_daytask_options a on d.id = a.qid where d.id ='.$id.' order by d.id limit 1';
		$dayTask = Db::query($sql);
		return ['dayTask'=>$dayTask];
	}
	
    //确定答案是否正确
	public function comfirmAnswer($answer)
	{
      //$uid = Request::instance()->header()['uid'];
        $uid = 2;
		if($answer == $this->answer){
			if($nowGrass >= 4){
				$nowGrass -= 3;
				$sheep += 1;
                $grassTotal += 1;
				$data = array('nowGrass'=>$nowGrass,'sheep'=>$sheep);
				$res = Db::table('xmr_daytask')->where('id',$id)->isUpdate(true)->save($data);
			}else{
				$nowGrass += 1; 
				$grassTotal += 1;
				$data = array('nowGrass'=>$nowGrass,'sheep'=>$sheep);
				$res =  Db::table('xmr_daytask')->where('id',$id)->isUpdate(true)->save($data);
			}
			$this->success('恭喜您获得一棵草');
		}else{
			$this->error('答错了');
		}
	}
}
?>